import user.User;
import user.Return;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/login/dgut")
public class Servlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpSession session = request.getSession(true);
        String token = (String) session.getAttribute("token");

        StringBuffer data1 = new StringBuffer();
        data1.append("token="+token+"&");
        data1.append("appid=" +"javaee"+"&");
        data1.append("appsecret=" +"b3b52e43ccfd"+"&");
        data1.append("userip=" + request.getLocalAddr());
        HttpPost httpPost = new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken"+ "?" +data1);

        CloseableHttpResponse res = httpClient.execute(httpPost);;
        HttpEntity httpentity1 = res.getEntity();
        while(httpentity1 == null)
        {
            httpPost = new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken"+ "?" +data1);
            res = httpClient.execute(httpPost);
            httpentity1 = res.getEntity();
        }

        String str = EntityUtils.toString(httpentity1, "utf-8");
        Jsonb jsonb = JsonbBuilder.create();
        //字符串转为对象
        Return JSON = jsonb.fromJson(str, Return.class);

        if(JSON != null && JSON.getmessage().equals("success"))
        {

            session.setAttribute("message", JSON.getmessage());
            StringBuffer data2 = new StringBuffer();
            String openid = JSON.getopenid();
            String access_token = JSON.getaccess_token();

            data2.append("openid="+openid+"&");
            data2.append("access_token="+access_token);

            HttpPost httpPost2 = new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo"+ "?" +data2);

            res = httpClient.execute(httpPost2);
            HttpEntity entity = res.getEntity();
            while(entity == null) {
                httpPost2 = new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo" + "?" + data2);
                res = httpClient.execute(httpPost2);
                entity = res.getEntity();
            }

            String str2 = EntityUtils.toString(entity, "utf-8");
            User JSON1 = jsonb.fromJson(str2, User.class);
            session.setAttribute("userName",JSON1 );
            response.sendRedirect("/login.jsp");
        }
        else
        {
            response.sendRedirect("https://cas.dgut.edu.cn?appid=javaee&state=STATE");
            return;
        }


    }


}
